<?php

//	$user_id = 2;
//	$book_id = 2;
//	$page_number = 1;
//	$page_number = "p" . $page_number;
//	$data = "<hhaa/><sub7/>";

	$user_id = $_GET['user_id'];
	$book_id = $_GET['book_id'];
	$page_number = $_GET['page_number'];
	$page_number = "p" . $page_number;
	$data = $_GET['data'];
	
	//echo $field_name . "=".$content;username="sub7" password="subsubsub"
	$dbh = new PDO('mysql:host=localhost;dbname=sbdb', 'root', 'winwinwin');
//update tblbook_data set p1 = "<dsdsd><adad>" where book_id=2
	$sql = "UPDATE tblbook_data SET ".$page_number . "=\"". $data. "\" WHERE book_id=".$book_id;
//	echo $sql;
	if(verifyUserIDAndBookID($dbh,$user_id,$book_id))
	{ // ok
		$dbh->exec("SET NAMES UTF8");
		$count = $dbh->exec($sql);
		echo $count;
	}
	else{ // error
		echo "<error> Book_id: ".$book_id ." doesn't exist on user_id: ".$user_id . " </error>";
	}
	
	

	// verify that does this user have this book_id or not
	function verifyUserIDAndBookID($dbh,$user_id,$book_id)
	{
		foreach($dbh->query('select count(*) as num from tblbook_id where user_id='.$user_id. ' and book_id='.$book_id) as $row)
		{
			if($row['num'] == 1)
				return true;
			else
				return false;
		}
	}
?>
